<?php
/*
 * 足球阵型采集
 */

namespace app\sports\jc\service;

use think\Db;
use think\Exception;
use app\common\HTTP;
use app\common\OSS;
use think\facade\Log;
use app\common\InstanceTrait;
use app\sports\jc\model\JcCaijiModel;

class JcCaiji
{
    private $user = '';
    private  $secret ='';
//    接口名:竞彩比赛指数
//    接口频次:3-5分钟
    private $url = 'open.sportnanoapi.com/api/v2/sports/jc/odds';

    protected function __construct()
    {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
    }

    /*
     * 采集竞彩指数
     */
    public function Execs()
    {
        try {
            $jcressult = json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret"), true);
//            竞彩足球
            $zuqiu = [];
            foreach ($jcressult['jczq'] as $value){
                $zuqiu[]=$value['id'];
            }
//            查询数据库
            $modelresult=JcCaiji::getInstance()->QueryIn($zuqiu);
            $addjcdata=[];
            foreach ($jcressult['jczq'] as $value){
                foreach ($modelresult as $v){
                    if ($value['id']==$v['nami_id']){
//                        更新数据
                        $data=[
                            'issue_num'   =>   $value['issue_num'],  //赛事编号
                            'spf'         =>   $value['spf'],
                            'rq'          =>   $value['rq'],
                        ];
                      JcCaiji::getInstance()->UpdateJc($v['id'],$data);
                    }else{
//                        新增数据
                        $addjcdata[]=[
                            'comp'        =>$value['comp'],
                            'issue_num'   =>$value['issue_num'],
                            'match_time'  =>$value['match_time'],
                            'short_home'  =>$value['short_home'],
                            'short_away'  =>$value['short_away'],
                            'spf'         =>$value['spf'],
                            'result_spf'  =>'',
                            'result_rq'   =>'',
                            'rq'          =>$value['rq'],
                            'create_time' =>date('Y-m-d H:i:s',time()),
                            'type'        =>'jczq 足球',
                            'nami_id'     =>$value['id'],
                        ];
                    }
                }
            }
//          竞彩篮球
            $lanqiu=[];
            foreach ($jcressult['jclq'] as $value){
                 $lanqiu[]=$value['id'];
            }
//          查询数据库
            $modelresult=JcCaiji::getInstance()->QueryIn($lanqiu);
            $addjcdata=[];
            foreach ($jcressult['jclq'] as $value){
                foreach ($modelresult as $v){
                    if ($value['id']==$v['nami_id']){
//                        更新数据
                        $data=[
                            'issue_num'   =>   $value['issue_num'],  //赛事编号
                            'spf'         =>   $value['spf'],
                            'rq'          =>   $value['rq'],
                        ];
                      JcCaiji::getInstance()->UpdateJc($v['id'],$data);
                    }else{
//                        新增数据
                        $addjcdata[]=[
                            'comp'        =>$value['comp'],
                            'issue_num'   =>$value['issue_num'],
                            'match_time'  =>$value['match_time'],
                            'short_home'  =>$value['short_home'],
                            'short_away'  =>$value['short_away'],
                            'spf'         =>$value['spf'],
                            'result_spf'  =>'',
                            'result_rq'   =>'',
                            'rq'          =>$value['rq'],
                            'create_time' =>date('Y-m-d H:i:s',time()),
                            'type'        =>'jczq 篮球',
                            'nami_id'     =>$value['id'],
                        ];
                    }
                }
            }
            //判断有数据才执行
            if (isset($addjcdata) && (!empty($addjcdata))){
                JcCaiji::getInstance()->InsertJcAll($addjcdata);
            }

            Log::log('info','竞彩数据采集完成');

        }catch(Exception $e){
            Log::error('足球竞彩采集错误:'.$e->getMessage());
            return [];
        }


    }
}